package com.zuowei.ycy.user.ui.mine.balance;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.adapter.ShouzhiAdapter;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.ShouZhiInfo;
import com.zuowei.ycy.user.http.Member;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;
import com.zuowei.ycy.user.util.PtrInitHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by admin on 2016/4/20.
 * 收支明细
 */
public class ShouzhiMingxiAty extends BaseToolbarAty implements PtrHandler, LoadMoreHandler {
    @Bind(R.id.listview)
    ListView listView;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;

    private List<ShouZhiInfo> list;
    private ShouzhiAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.shouzhi_mingxi_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("收支明细");
        PtrInitHelper.initPtr(this, ptrFrame);
        ptrFrame.setPtrHandler(this);
        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(false);
        loadMoreListViewContainer.loadMoreFinish(false, false);
        loadMoreListViewContainer.setLoadMoreHandler(this);

        list = new ArrayList<>();

        adapter = new ShouzhiAdapter(this, list, R.layout.shouzhi_mingxi_item);
        listView.setAdapter(adapter);
        listView.setEmptyView(llEmpty);
    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    @Override
    public void requestData() {
        showLoadingContentDialog();
        doHttp(RetrofitUtils.createApi(Member.class).balancePayments(UserManger.getM_id()), 0);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        doHttp(RetrofitUtils.createApi(Member.class).balancePayments(UserManger.getM_id()), 0);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        doHttp(RetrofitUtils.createApi(Member.class).balancePayments(UserManger.getM_id()), 1);
    }



    @Override
    public void onFailure(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what == 0) {
            isShowToast = false;
            ptrFrame.refreshComplete();
        } else if (what == 1) {
            isShowToast = false;
            loadMoreListViewContainer.setAutoLoadMore(false);
            loadMoreListViewContainer.loadMoreFinish(false, false);
        }
        super.onFailure(result, call, response, what);
        isShowToast = true;
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what==0){
            list.clear();
            list.addAll(AppJsonUtil.getArrayList(result,ShouZhiInfo.class));
            adapter.notifyDataSetChanged();
            ptrFrame.refreshComplete();
        }else {
            list.addAll(AppJsonUtil.getArrayList(result,ShouZhiInfo.class));
            adapter.notifyDataSetChanged();
            loadMoreListViewContainer.setAutoLoadMore(false);
            loadMoreListViewContainer.loadMoreFinish(false, true);
        }
        super.onSuccess(result, call, response, what);
    }
}
